/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package com.zgw.his.design.parser;

import java.util.List;

import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.wb.internal.core.model.property.Property;
import org.eclipse.wb.internal.core.model.property.category.PropertyCategory;
import org.eclipse.wb.internal.core.model.property.editor.StringArrayPropertyEditor;
import org.eclipse.wb.internal.core.model.property.table.PropertyTooltipProvider;
import org.eclipse.wb.internal.core.model.property.table.PropertyTooltipTextProvider;
import org.eclipse.wb.internal.core.model.util.ScriptUtils;
import org.eclipse.wb.internal.core.xml.model.EditorContext;
import org.eclipse.wb.internal.core.xml.model.XmlObjectInfo;
import org.eclipse.wb.internal.core.xml.model.description.ComponentDescription;
import org.eclipse.wb.internal.core.xml.model.description.DescriptionPropertiesHelper;
import org.eclipse.wb.internal.core.xml.model.description.GenericPropertyDescription;
import org.eclipse.wb.internal.core.xml.model.description.IDescriptionProcessor;
import org.eclipse.wb.internal.core.xml.model.property.accessor.ExpressionAccessor;

import com.zgw.his.design.model.property.edit.ElementCodePropertyEditor;
import com.zgw.his.design.model.property.edit.InputSelectedPropertyEditor;

/**
 * 用于修改 模版控件属性编辑器等
 * */
public class MedicalDescriptionProcessor implements IDescriptionProcessor {
	// //////////////////////////////////////////////////////////////////////////
	//
	// Instance
	//
	// //////////////////////////////////////////////////////////////////////////
	public static final IDescriptionProcessor INSTANCE = new MedicalDescriptionProcessor();

	private MedicalDescriptionProcessor() {
	}

	// //////////////////////////////////////////////////////////////////////////
	//
	// IDescriptionProcessor
	//
	// //////////////////////////////////////////////////////////////////////////
	public void process(EditorContext context,
			ComponentDescription componentDescription) throws Exception {
		if (isXWT(context)) {
			useProperty(componentDescription);
			// addProperties_TableViewerColumn(componentDescription);
		}
	}

	/**
	 * 自定义属性编辑
	 */
	private void useProperty(ComponentDescription componentDescription) {
		List<GenericPropertyDescription> properties = componentDescription
				.getProperties();
		for (GenericPropertyDescription property : properties) {
			if (property.getName().equals("code")) {
				property.setEditor(ElementCodePropertyEditor.INSTANCE);
				property.putTag("title", ElementCodePropertyEditor.TITLE);
				property.setCategory(PropertyCategory.PREFERRED);
			} else if (property.getName().equals("inputSelectedCode")) {
				property.setEditor(InputSelectedPropertyEditor.INSTANCE);
				property.putTag("title", InputSelectedPropertyEditor.TITLE);
			}
		}
	}

	// //////////////////////////////////////////////////////////////////////////
	//
	// Utils
	//
	// //////////////////////////////////////////////////////////////////////////
	/**
	 * @return <code>true</code> if given {@link XmlObjectInfo} is XWT.
	 */
	public static boolean isXWT(XmlObjectInfo object) {
		EditorContext context = object.getContext();
		return isXWT(context);
	}

	/**
	 * @return <code>true</code> if given {@link EditorContext} is XWT.
	 */
	public static boolean isXWT(EditorContext context) {
		return context.getToolkit().getId().equals("org.eclipse.wb.rcp");
	}
}
